package com.manager.user.module.service;

import com.manager.commons.base.DataGrid;
import com.manager.commons.mybatis.dao.GenericDao;
import com.manager.commons.mybatis.service.GenericService;
import com.manager.commons.utils.PinyinUtil;
import com.manager.commons.utils.StringUtil;
import com.manager.commons.utils.UUIDUtil;
import com.manager.core.module.manager.entity.ComboTreeList;
import com.manager.core.web.json.AjaxJson;
import com.manager.user.module.dao.GenOrganizationDao;
import com.manager.user.module.entity.GenOrganization;
import com.manager.user.utils.CodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * GenOrganizationService
 * 组织架构
 *
 * @author manager
 * @version 1.0
 */
@Service
public class GenOrganizationService extends GenericService<GenOrganization> {

    @Autowired
    private GenOrganizationDao jdbcDao;

    @Override
    public GenericDao<GenOrganization> getDao() {
        return jdbcDao;
    }

    /**
     * 保存
     *
     * @param params
     * @param index
     * @return
     */
    public AjaxJson save(Map<String, Object> params, int index) {
        try {
            if (null != params) {
                String _id = String.valueOf(params.get("_id"));
                String parent_code = getParamsString(params, "parent_code");
                String name = getParamsString(params, "name");
                String code = PinyinUtil.getFirstLetter(name);
                code = CodeGenerator.generateOrderNumber(code);
                params.put("code", code);
                if (StringUtil.isEmpty(parent_code)) {
                    params.put("level", 1);
                    params.put("levelCode", code);
                } else {
                    params.put("parentId", parent_code);
                    GenOrganization organization = jdbcDao.findModel(parent_code);
                    if (organization != null && StringUtil.isNotEmpty(organization.getLevelCode())) {
                        params.put("levelCode", organization.getLevelCode() + "-" + code);
                    }
                }
                int f = 0;
                if (StringUtil.isEmpty(_id)) {
                    String uuid = UUIDUtil.getInstance().getUUID();
                    if (index > 0) {
                        uuid = UUIDUtil.getInstance().getUUIDDate(index);
                    }
                    params.put("_id", uuid);
                    params.put("id", uuid);

                    f = insertMap(params);
                } else {
                    params.put("id", _id);
                    f = updateMap(params);
                }
                return new AjaxJson(f, "保存".concat(f > 0 ? "成功" : "失败"));
            }
            return new AjaxJson("参数错误");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return new AjaxJson("异常错误，请稍后再试");
    }

    /**
     * 分页查询
     *
     * @param params
     * @return
     */
    public DataGrid queryPage(Map<String, Object> params) {
        try {
            if (null != params) {
                int count = jdbcDao.countMap(params);
                List<GenOrganization> list = jdbcDao.queryMap(params);
                queryChildren(list, params);
                return new DataGrid(count, list);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return new DataGrid(0, new ArrayList<GenOrganization>());
    }

    private void queryChildren(List<GenOrganization> list, Map<String, Object> params) {
        for (GenOrganization gtc : list) {
            params.put("parent_code", gtc.getId());
            List<GenOrganization> l = jdbcDao.queryMap(params);
            if (l != null && !l.isEmpty()) {
                gtc.setChildren(l);
                queryChildren(l, params);
            }
        }
    }

    public List<ComboTreeList> queryTreeList(Map<String, Object> params) {
        return jdbcDao.queryTree(params);
    }
}
